package com.jiyici.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ScrollView;

import com.jiyici.utils.DisplayUtil;

public class AutoFitScrollView extends ScrollView {

    public AutoFitScrollView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public AutoFitScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public AutoFitScrollView(Context context) {
        super(context);
    }
    
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        View child = getChildAt(0);
        child.measure(widthMeasureSpec, heightMeasureSpec);
        int childHeight  = child.getMeasuredHeight();
        int childWidth = child.getMeasuredWidth();
        
        int maxHeight = DisplayUtil.dip2px(getContext(), 300);
//        LogUtil.info("autofit", "childe height = " + childHeight + ", width = " + childWidth + ", maxHeight = " + maxHeight);
        if (childHeight > maxHeight)
            childHeight = maxHeight;
        setMeasuredDimension(childWidth, childHeight);
    }

}
